#include <Windows.h>
#include "WinApi.h"

void * __cdecl operator new(size_t size)
{
	return API(KERNEL32, HeapAlloc)(API(KERNEL32, GetProcessHeap)(), HEAP_ZERO_MEMORY, size);
}

void * __cdecl operator new[](size_t size)
{
	return API(KERNEL32, HeapAlloc)(API(KERNEL32, GetProcessHeap)(), HEAP_ZERO_MEMORY, size);
}

void __cdecl operator delete[](void * lpMem)
{
	API(KERNEL32, HeapFree)(API(KERNEL32, GetProcessHeap)(), 0, lpMem);
}

void __cdecl operator delete(void * lpMem)
{
	API(KERNEL32, HeapFree)(API(KERNEL32, GetProcessHeap)(), 0, lpMem);
}

void __cdecl operator delete(void * lpMem, size_t nSize)
{
	API(KERNEL32, HeapFree)(API(KERNEL32, GetProcessHeap)(), 0, lpMem);
}

void __cdecl operator delete[](void * lpMem, size_t nSize)
{
	API(KERNEL32, HeapFree)(API(KERNEL32, GetProcessHeap)(), 0, lpMem);
}


extern "C"
{
#pragma function(memcpy)
	void * __cdecl memcpy(void* pDest, const void* pSrc, size_t nCount) {
		if (pDest == 0 || pSrc == 0)
		{
			return 0;
		}
		char* pD = (char*)pDest;
		char* pS = (char*)pSrc;
		while (nCount--)
		{
			*pD++ = *pS++;
		}

		return pDest;
	}

#pragma function(memset)
	void * __cdecl memset(void *pDest, int nValue, size_t nLen) {
		unsigned char *p = (unsigned char *)(pDest);
		while (nLen-- > 0) {
			*p++ = nValue;
		}

		return pDest;
	}

}
